<template>
    <div style="height:100%;width:100%;position:relative;" ref="maindev">
        <div class="pagetop">
            <table style="width:100%">
                <tr>
                    <td>日期范围</td>
                    <td style="width: 280px">
                        <el-date-picker v-model="date1" type="date" value-format="yyyy-MM-dd" size="mini" placeholder="开始日期" style="width:130px" :clearable="false"></el-date-picker> -
                        <el-date-picker v-model="date2" type="date" value-format="yyyy-MM-dd" size="mini" placeholder="结束日期" style="width:130px" :clearable="false"></el-date-picker>
                    </td>
                    <td>报告单元</td>
                    <td style="width: 160px">
                        <rm-ddl-rptunit defaultflag="1" v-model="rptunitid" reporttype="autochk" width="160px" :clearable="false" ref="rptunit" @change="rptunitChange"></rm-ddl-rptunit>
                    </td>
                    <td>患者类型</td>
                    <td style="width: 160px">
                        <el-select v-model="pattypecode" width="160px">
                            <el-option v-for="item in  PTOption" :key="item.hidkey" :label="item.hidname" :value="item.hidkey"></el-option>
                            <el-option label="全部" value=""></el-option>
                        </el-select>
                    </td>
                    <td>送检科室</td>
                    <td style="width: 160px">
                        <el-select v-model="req_deptno" width="160px" filterable :filter-method="deptnameChange">
                            <el-option v-for="item in  deptOption" :key="item.hidkey" :label="item.hidname" :value="item.hidkey"></el-option>
                            <el-option label="全部" value=""></el-option>
                        </el-select>
                    </td>
                </tr>
                <tr>
                    <td>自动审核结果</td>
                    <td colspan="5">
                        <el-select v-model="autochkresult" width="160px">
                            <el-option label="通过" value="1"></el-option>
                            <el-option label="不通过" value="2"></el-option>
                            <el-option label="全部" value=""></el-option>
                        </el-select>
                        <span style="font-size: 12px;padding-left: 10px">人工判断</span>
                        <el-select v-model="reviewflag" width="160px">
                            <el-option label="相符" value="1"></el-option>
                            <el-option label="不相符" value="2"></el-option>
                            <el-option label="全部" value=""></el-option>
                        </el-select>
                    </td>
                </tr>
                <tr>
                    <td colspan="6" style="text-align:left;font-size: 18px">
                        根据您设置的条件，该时间段内共验证<span style="color:blue;font-weight:bold;padding:0 5px 0 5px">{{ samplecount }}</span>个标本, 自动审核通过率<span style="color:#abcf93;font-weight:bold;padding:0 5px 0 5px">{{ autorate }}</span>%，人工判断通过率<span style="color:#abcf93;font-weight:bold;padding:0 5px 0 5px">{{ reviewrate }}</span>%
                    </td>
                </tr>
                <tr>
                    <td colspan="11"></td>
                    <td>
                        <div style="width:100%;text-align:right;dispaly:block">
                            <el-button @click.native="tablePage.currentPage = 1,QueryData(1)" type="primary" v-has="'PINQUERY'" icon="el-icon-search">查询</el-button>
                            <el-button @click.native="OutData('1')" v-has="'PEXPORT'" icon="el-icon-upload2">导出</el-button>
                            <el-button @click.native="OutData('2')" v-has="'PPRINT'" icon="el-icon-printer">打印</el-button>
                            <el-button @click.native="OutData('3')" v-has="'PPRINT'" icon="el-icon-edit-outline">样式设计</el-button>
                        </div>
                    </td>
                </tr>
            </table>

        </div>
        <div class="pagecenter">
            <vxe-table show-overflow ref="xTable" height="90%" :loading="loading" :data.sync="tableData" :scroll-y="{rHeight:'50px'}" :start-index="(tablePage.currentPage - 1) * tablePage.pageSize" border size="mini" header-align="center" show-header-overflow highlight-current-row resizable stripe>
                <vxe-table-column field="pat_name" title="姓名" width="60" sortable></vxe-table-column>
                <vxe-table-column field="pat_agestr" title="年龄" width="40" sortable header-align="center" align="center"></vxe-table-column>
                <vxe-table-column field="pat_sexname" title="性别" width="40" sortable header-align="center" align="center"></vxe-table-column>
                <vxe-table-column field="pat_no" title="门诊/住院号" width="80" sortable header-align="center" align="center"></vxe-table-column>
                <vxe-table-column field="req_deptnoname" title="送检科室" width="80" sortable header-align="center" align="center"></vxe-table-column>
                <vxe-table-column field="req_reason" title="申请项目" width="160" sortable header-align="center"></vxe-table-column>
                <vxe-table-column field="cp_rptitem" title="报告项目" width="200" sortable header-align="center" :show-overflow="false">
                    <template v-slot="{ row }">
                        <div>
                            <span v-for="(item,index) in row.cp_rptitem.split(',')" :key="index" :style="{ color: row.chkresult.indexOf(item.split(':')[0]) > -1 ? '#ff2f2f':'', padding: '0 5px 0 0' }">{{ item }}</span>
                        </div>
                    </template>
                </vxe-table-column>
                <vxe-table-column field="chkresult" title="自动审核结果" width="200" sortable header-align="center">
                    <template v-slot="{ row }">
                        <div v-for="(item,index) in row.chkresult.split(';')" :key="index" :style="{ textAlign: index == 0?'center':'left', color: row.chkflag == '2'?'#ff2f2f':'' }">{{ item }}</div>
                    </template>
                </vxe-table-column>
                <vxe-table-column field="reviewresult" title="人工判断" width="160" sortable header-align="center">
                    <template v-slot="{ row }">
                        <div v-for="(item,index) in row.reviewresult.split(';')" :key="index" :style="{ textAlign: index == 0?'center':'left', color: row.review_flag == '2'?'#ff2f2f': '' }">{{ item }}</div>
                    </template>
                </vxe-table-column>
                <vxe-table-column field="rechk_username" title="审核人" width="80" sortable header-align="center" align="center"></vxe-table-column>
                <vxe-table-column field="rechk_dt" title="审核时间" width="120" sortable header-align="center" align="center"></vxe-table-column>
            </vxe-table>
            <vxe-pager :loading="loading" :current-page="tablePage.currentPage" :page-size="tablePage.pageSize" :total="tablePage.totalResult" :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']" @page-change="handlePageChange"></vxe-pager>
        </div>

    </div>
</template>

<script>
import XE from "xe-utils"; //引入工具类
export default {
    name: "LIS416",
    data() {
        return {
            date1: "",
            date2: "",
            rptunitid: 0,
            loading: false,
            pattypecode: "",
            req_deptno: "",
            deptOption: [],
            deptOptionAll: [],
            PTOption: [],
            autochkresult: "",
            reviewflag: "",
            tableData: [],
            samplecount: 0, //验证标本数量
            autorate: 0, //自动审核通过率
            reviewrate: 0, //人工判断通过率
            tablePage: {
                currentPage: 1,
                pageSize: 50,
                totalResult: 0
            },
            rptunitname: "",
        };
    },
    created() {
        this.InitOption();
    },
    mounted() {},
    updated() {},
    methods: {
        InitOption() {
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS416",
                    method: "post",
                    data: {
                        action: "Init"
                    }
                })
                .then(res => {
                    if (res.errcode == "1") {
                        this.deptOption = res.Dept;
                        this.deptOptionAll = res.Dept;
                        this.PTOption = res.PT;
                        this.date1 = res.time;
                        this.date2 = res.time;
                    }
                });
        },
        QueryData(type) {
            this.loading = true;
            if (type == 1) {
                this.tableData = [];
                this.samplecount = 0;
                this.tablePage.totalResult = 0;
                this.autorate = 0.0;
                this.reviewrate = 0.0;
            }
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS416",
                    method: "post",
                    data: {
                        action: type == 1 ? "query" : "querypage",
                        date1: this.date1,
                        date2: this.date2,
                        rptunitid: this.rptunitid,
                        pattypecode: this.pattypecode,
                        reqdeptno: this.req_deptno,
                        autoresult: this.autochkresult,
                        reviewflag: this.reviewflag,
                        PageSize: this.tablePage.pageSize,
                        currentPage: this.tablePage.currentPage
                    }
                })
                .then(res => {
                    if (res.errcode == "1") {
                        this.tableData = res.Data;
                        if (type == 1) {
                            this.samplecount = res.StaData[0].num;
                            this.tablePage.totalResult = res.StaData[0].shownum;
                            var throughnum = res.StaData[0].chknum;
                            var reviewnum = res.StaData[0].reviewnum;
                            this.autorate = ((throughnum * 100) / this.samplecount).toFixed(2);
                            this.reviewrate = ((reviewnum * 100) / this.samplecount).toFixed(2);
                        }
                    }
                    this.loading = false;
                })
                .catch(err => {
                    this.loading = false;
                });
        },
        OutData(type) {
            this.$tool
                .request({
                    url: "LIS/LIS4/LIS416",
                    method: "post",
                    timeout: 600000,
                    data: {
                        action: "outdate",
                        date1: this.date1,
                        date2: this.date2,
                        rptunitid: this.rptunitid,
                        pattypecode: this.pattypecode,
                        reqdeptno: this.req_deptno,
                        autoresult: this.autochkresult,
                        reviewflag: this.reviewflag
                    }
                })
                .then(res => {
                    if (res.errcode == "1") {
                        if (type == "1") {
                            this.BtnExport(res.data);
                        } else if (type == "2") {
                            this.btnPrint(res.data);
                        } else {
                            this.RptStyleSetting(res.data);
                        }
                    }
                })
                .catch(err => {
                    this.$alert(err, "错误");
                });
        },
        handlePageChange({ currentPage, pageSize }) {
            this.tablePage.currentPage = currentPage;
            this.tablePage.pageSize = pageSize;
            this.QueryData(2);
        },
        deptnameChange(val) {
            if (val) {
                this.deptOption = this.deptOptionAll.filter(item => item.hidname == val || item.hidkey == val);
            } else {
                this.deptOption = this.deptOptionAll;
            }
        },
        rptunitChange(code, name) {
            this.rptunitid = code;
            this.rptunitname = name;
        },
        BtnExport(data) {
            var tableColumn = this.$refs.xTable.getColumns();
            var extableColumn = [];
            tableColumn.forEach(item => {
                extableColumn.push({ title: item.title, field: item.property });
            });
            let filtertxt = this.getExportFilter();
            this.$pub.exportTableExcel(data, extableColumn, "LIS416 自动审核验证明细.xlsx", filtertxt);
        },
        // 获取当前导出文件的所有查询条件
        getExportFilter() {
            let filtertxt = "日期范围:" + XE.toDateString(this.date1, "yyyy-MM-dd") + "至" + XE.toDateString(this.date2, "yyyy-MM-dd ") + "; ";
            if(this.rptunitid) filtertxt += "报告单元:" + this.rptunitname + "; ";
            if(this.pattypecode) filtertxt += "患者类型:" + this.getOptionName(this.PTOption.filter((item) =>  this.pattypecode == item.hidkey)) + "; ";
            else filtertxt += "患者类型:全部; ";
            if(this.req_deptno) filtertxt += "送检科室:" + this.getOptionName(this.deptOption.filter((item) =>  this.req_deptno == item.hidkey)) + "; ";
            else filtertxt += "送检科室:全部; ";
            filtertxt += "自动审核结果:" + (this.autochkresult == "1" ? "通过" : this.autochkresult == "2" ? "不通过" : "全部") + "; ";
            filtertxt += "人工判断:" + (this.reviewflag == "1" ? "相符" : this.reviewflag == "2" ? "不相符" : "全部") + "; ";
            return filtertxt;
        },
        getOptionName(option) {
            if (option && option.length > 0) {
                let optionname = "";
                option.forEach((item) => {
                    if(item.hasOwnProperty("hidname")) {
                        optionname += item.hidname + ",";
                    } else {
                        optionname += item.label + ",";
                    }
                });
                return optionname.substring(0, optionname.length - 1);
            } else {
                return "";
            }
        },
        btnPrint(data) {
            if (data.length > 0) {
                const rload = this.$pub.openLoading(this, "正在打印，请稍候……");
                let column = this.$refs.xTable.getTableColumn().tableColumn;
                let str = JSON.stringify(this.$pub.InitColumn(column));
                str += "\r";
                str += JSON.stringify(data);
                let sql = btoa(encodeURIComponent(str));
                jsObj.PrintCustomJsonReport("5057", sql, this.$tool.getSession(this.$tool.loginuserno), "自动审核明细", "");
                rload.close();
            } else {
                this.$alert("当前没有要打印的数据！");
            }
        },
        RptStyleSetting(data) {
            if (data.length > 0) {
                let column = this.$refs.xTable.getTableColumn().tableColumn;
                let str = JSON.stringify(this.$pub.InitColumn(column));
                str += "\r";
                str += JSON.stringify(data);
                let sql = btoa(encodeURIComponent(str));
                jsObj.SetCustomJsonPrintConfig("5057", sql, this.$tool.getSession(this.$tool.loginuserno), "自动审核明细");
            } else {
                this.$alert("当前没有要打印的数据！");
            }

            // for (let i = 0; i < data.length; i++) {
            //     data[i].chkresult = data[i].chkresult.replace(/;/g, "\n");
            //     data[i].reviewresult = data[i].reviewresult.replace(/;/g, "\n");
            //     data[i].cp_rptitem = data[i].cp_rptitem.replace(/,/g, " ");
            // }
        }
    }
};
</script>

<style scoped>
.pagetop {
    width: 100%;
    height: 120px;
    display: -webkit-box;
    -webkit-box-pack: left;
    -webkit-box-align: center;
    border: 1px solid #ccc;
    position: absolute;
    z-index: 99;
}

.pagecenter {
    position: absolute;
    top: 124px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}
table tr td:nth-child(1) {
    width: 80px;
}
table tr td:nth-child(odd) {
    text-align: right;
    white-space: nowrap;
    font-size: 12px;
    width: 60px;
}
</style>